<template>
  <!-- gutter 设置间距 -->
  <el-row class="home" :gutter="50">
    <!-- :span 设置宽度  一行最多24列 -->
    <el-col :span="8" class="left">
      <el-card shadow="hover">
        <div class="user">
          <div class="avatar">
            <!-- 头像 -->
            <el-avatar shape="square" :size="150" fit="cover" :src="Uistore.getAvatarUrl('avatar2')" />
          </div>
          <!-- 个人信息展示 -->
          <div class="user-info">
            <p>Admin</p>
            <p>超级管理员</p>
          </div>
        </div>

        <!-- 登录信息展示 -->
        <div class="login-info">
          <div class="info-item">
            <span class="login-time-span">上次登录时间：</span>
            <p class="login-time-p">2025-01-01 12:00:00</p>
          </div>
          <div class="info-item">
            <span class="login-place-span">上次登录地点：</span>
            <p class="login-place-p">192.168.1.1</p>
          </div>
        </div>
      </el-card>

      <el-card shadow="hover" class="table">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column fixed prop="brands" label="品牌" width="100px" />
          <el-table-column prop="todayPurchases" label="日销量" width="100px" />
          <el-table-column prop="monthPurchases" label="月销量" width="100px" />
          <el-table-column prop="totalPurchases" label="总销量" width="100px" />
        </el-table>
      </el-card>
    </el-col>


    <el-col :span="16" class="right">
      <el-card shadow="hover" class="experienceInfo">
        <el-row>
          <el-col :span="8" v-for="item in experienceInfo" :key="item.icon" class="statistic">
            <el-statistic :title="item.title" :value="item.value" :value-style="{ color: '#3f8600' }"
              style="margin-right: 50px">
              <el-icon>
                <component :is="item.icon" />
              </el-icon>
            </el-statistic>
          </el-col>
        </el-row>
      </el-card>

      <el-card class="chart" shadow="hover">
         <div class="chartContainer" ref="chartContainer">

         </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { defineOptions, ref, onMounted} from 'vue'
defineOptions({
  name: 'HomeIndex'
})
import * as echarts from 'echarts';
import { useUIStore } from '@/stores/UI'
import { getExperienceInfoMethod, getTableDataMethod ,getChartDataMethod} from '@/utils/methods'
import { ElMessage } from 'element-plus';
const Uistore = useUIStore()
// import { tableData } from '@/miscellaneous/index'
const tableData = ref([])
const experienceInfo = ref([])
const chartContainer = ref(null)
let myChart = null
const initChart = async () => {
  let option = null
  try {
    const res = await getChartDataMethod()
    option = res.data

  }
  catch (error){
    ElMessage.error(error.message)
    return
  }
  if (!chartContainer.value) {
    return
  }
  // 初始化图表
  myChart = echarts.init(chartContainer.value)
  myChart.setOption(option)

 }


onMounted(() => {
  // 获取表格数据
  getTableDataMethod().then(res => {
    tableData.value = res.data
  })
  // 获取合计信息
  getExperienceInfoMethod().then(res => {
    experienceInfo.value = res.data
  })
  // 获取图表数据
  initChart()
});

</script>

<style scoped lang="scss">
.home {
  height: 100%;
  background: #f5f7fa;
  padding: 20px;
}

.user {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .user-info {
    // 方法1：使用margin-right让userinfo向左移动
    margin-right: 1.6rem; // 调整这个值来控制向左移动的距离
  }
}

// 登录信息样式
.login-info {
  margin-top: 20px;
  padding: 15px;
  border-top: 1px solid #2f4c8f;

  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  span {
    font-size: 14px;
    color: #909399;
    margin-right: 8px;
    white-space: nowrap;
  }

  p {
    font-size: 14px;
    color: #606266;
    margin: 0;
    flex: 1;
  }

  .login-time-p {
    font-weight: 500;
    color: #6f63c2;
  }

  .login-place-p {
    color: #409eff;
  }
}

.table {
  margin-top: 1rem;
}
.statistic{
  margin-bottom: 1.6rem
}
 :deep(.el-statistic__head) {
  font-size: 1rem;
  color: #897cb6;
}
.chart{
  margin-top: 1.6rem;
}
.chartContainer{
  width: 100%;
  height: 400px;
}
</style>
